package com.djzz.app.wdy.base;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.djzz.app.wdy.definations.keys.IntentKeys;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Fragment 基类
 * Created by hh on 2017/8/30.
 */

public abstract class BaseFragment extends Fragment {

    protected Unbinder unbinder;
    protected View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        if(rootView == null){

            rootView = inflater.inflate(setLayoutId(), container, false);
            unbinder = ButterKnife.bind(this, rootView);
        }

        init();

        return rootView;
    }

    protected abstract int setLayoutId();
    protected abstract void init();

    protected void redirectTo(Class<? extends Activity> activity){
        redirectTo(activity, false);
    }

    protected void redirectTo(Class<? extends Activity> activity, boolean isFinish){
        redirectTo(activity, isFinish, null);
    }

    protected void redirectTo(Class<? extends Activity> activity, boolean isFinish, Bundle bundle){
        Intent intent = new Intent();
        intent.setClass(getActivity(),activity);
        if(bundle != null){
            intent.putExtra(IntentKeys.BUNDLE, bundle);
        }

        getActivity().startActivity(intent);

        if(isFinish){
            getActivity().finish();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}
